package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.data.Word;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.CommonTask.WordTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.TheWordsAdapter;
import com.iknow.ui.model.TheWordsAdapter.OnWordGroupRemoveCallBack;
import com.iknow.ui.model.TheWordsAdapter.OnWordRemoveCallback;
import com.iknow.util.MsgDialog;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.List;

public class MyWordBookActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private DialogInterface.OnClickListener BuckupDialogClickListener;
  private DialogInterface.OnClickListener RestoreDialogClickListener;
  private ExpandableListView eListView;
  private boolean mBinded;
  private Context mContext;
  private ServiceConnection mSvrConn;
  private CommonTask.WordTask mTask;
  private TaskListener mTaskListener;
  private TheWordsAdapter mTheWordsAdapter;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public MyWordBookActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.eListView = null;
    this.mTheWordsAdapter = null;
    this.RestoreDialogClickListener = new DialogInterface.OnClickListener(this)
    {
      public void onClick(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case -1:
        }
        while (true)
        {
          paramDialogInterface.dismiss();
          return;
          this.this$0.startSycn(4, null);
        }
      }
    };
    this.BuckupDialogClickListener = new DialogInterface.OnClickListener(this)
    {
      public void onClick(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case -1:
        }
        while (true)
        {
          paramDialogInterface.dismiss();
          return;
          this.this$0.startSycn(5, this.this$0.getWords());
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      protected ProgressDialog pd = null;

      public String getName()
      {
        return "WordTask";
      }

      public void onCancelled()
      {
        if (MyWordBookActivity.access$0(this.this$0) != null)
          MyWordBookActivity.access$0(this.this$0).cancel(true);
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
          MyWordBookActivity.access$4(this.this$0);
        while (true)
        {
          do
          {
            if (this.pd != null)
              this.pd.dismiss();
            return;
          }
          while (paramTaskResult != TaskResult.FAILED);
          MyWordBookActivity.access$5(this.this$0, ((CommonTask.WordTask)paramGenericTask).getMsg());
        }
      }

      public void onPreExecute()
      {
        if (MyWordBookActivity.access$0(this.this$0).getActionCode() == 0)
          this.pd = ProgressDialog.show(MyWordBookActivity.access$1(this.this$0), "", "正在加载数据,请稍候...", false);
        while (true)
        {
          return;
          this.pd = ProgressDialog.show(MyWordBookActivity.access$1(this.this$0), "", "正在同步云端数据，请稍候", false, true);
        }
      }

      public void onProgressUpdate(, Object paramObject)
      {
        MyWordBookActivity.access$2(this.this$0).setAdapter(MyWordBookActivity.access$3(this.this$0));
      }
    };
  }

  private void onSubmiteFinished()
  {
    if (this.mTask.getActionCode() == 0);
    while (true)
    {
      return;
      if (this.mTask.mActionCode == 2)
      {
        List localList = this.mTask.getWordList();
        IKnow.mIKnowDataBase.removeWord(localList);
      }
      onRefresh();
      Toast.makeText(this.mContext, "同步完成", 0).show();
    }
  }

  private void onSubmitefailure(String paramString)
  {
    List localList = this.mTask.getWordList();
    switch (this.mTask.getActionCode())
    {
    default:
    case 0:
    case 1:
    case 2:
    }
    while (true)
    {
      while (true)
      {
        onRefresh();
        Toast.makeText(this.mContext, paramString, 0).show();
        return;
        IKnow.mIKnowDataBase.addWord(localList);
        paramString = "已保存";
      }
      IKnow.mIKnowDataBase.removeWord(localList);
      paramString = "已删除";
    }
  }

  public boolean checkLogion()
  {
    int i;
    if (IKnow.mIKnowDataBase.getUser() == null)
    {
      Intent localIntent = new Intent(this.mContext, LoginActivity.class);
      this.mContext.startActivity(localIntent);
      i = 0;
    }
    while (true)
    {
      return i;
      i = 1;
    }
  }

  public List<Word> getWords()
  {
    return IKnow.mIKnowDataBase.getWord(0, 999);
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903103);
    this.mContext = this;
    this.eListView = ((ExpandableListView)findViewById(2131296478));
    this.mTheWordsAdapter = new TheWordsAdapter(this.mContext, this.eListView);
    this.mTheWordsAdapter.setOnWordRemoveCallback(new OnWordRemoveListener(this, null));
    this.mTheWordsAdapter.setOnWordGroupRemoveCallBack(new OnWordGroupRemoveListener(this, null));
    findViewById(2131296342).setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
    findViewById(2131296344).setVisibility(8);
    startSycn(0, null);
  }

  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    getMenuInflater().inflate(2131492868, paramMenu);
    MenuItem localMenuItem1 = paramMenu.getItem(0);
    MenuItem localMenuItem2 = paramMenu.getItem(1);
    localMenuItem1.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(this)
    {
      public boolean onMenuItemClick()
      {
        int i;
        if (!(IKnow.IsNetAviable()))
        {
          Toast.makeText(MyWordBookActivity.access$1(this.this$0), this.this$0.getString(2131361878), 0).show();
          i = 1;
        }
        while (true)
        {
          while (true)
          {
            return i;
            if (this.this$0.checkLogion())
              break;
            i = 1;
          }
          MsgDialog.showB2Dilog(MyWordBookActivity.access$1(this.this$0), this.this$0.getString(2131361936), MyWordBookActivity.access$7(this.this$0));
          i = 1;
        }
      }
    });
    localMenuItem2.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(this)
    {
      public boolean onMenuItemClick()
      {
        int i;
        if (!(IKnow.IsNetAviable()))
        {
          Toast.makeText(MyWordBookActivity.access$1(this.this$0), this.this$0.getString(2131361878), 0).show();
          i = 1;
        }
        while (true)
        {
          while (true)
          {
            return i;
            if (this.this$0.checkLogion())
              break;
            i = 1;
          }
          MsgDialog.showB2Dilog(MyWordBookActivity.access$1(this.this$0), this.this$0.getString(2131361935), MyWordBookActivity.access$8(this.this$0));
          i = 1;
        }
      }
    });
    return true;
  }

  protected void onDestroy()
  {
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    super.onPause();
  }

  public void onRefresh()
  {
    this.mTheWordsAdapter.init();
    this.mTheWordsAdapter.notifyDataSetChanged();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    super.onResume();
  }

  public void startSycn(int paramInt, List<Word> paramList)
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING) && (!(this.mTask.isCancelled())));
    while (true)
    {
      return;
      this.mTask = new CommonTask.WordTask(this.mContext);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.setWordList(paramList);
      this.mTask.setActionCode(paramInt);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("action", Integer.valueOf(paramInt));
      localTaskParams.put("xmpp", this.mXmppFacade);
      CommonTask.WordTask localWordTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localWordTask.execute(arrayOfTaskParams);
    }
  }

  private class OnWordGroupRemoveListener
  implements TheWordsAdapter.OnWordGroupRemoveCallBack
  {
    public void removeWordGroup()
    {
      String str = "删除" + paramString + "记录的所有词汇";
      MsgDialog.showB2Dilog(MyWordBookActivity.access$1(this.this$0), str, new DialogInterface.OnClickListener(this, paramString)
      {
        public void onClick(, int paramInt)
        {
          switch (paramInt)
          {
          default:
          case -1:
          }
          while (true)
          {
            return;
            List localList = IKnow.mIKnowDataBase.getWordList(this.val$groupName);
            MyWordBookActivity.OnWordGroupRemoveListener.access$1(this.this$1).startSycn(2, localList);
          }
        }
      });
    }
  }

  private class OnWordRemoveListener
  implements TheWordsAdapter.OnWordRemoveCallback
  {
    public void fireOnWordRemoved()
    {
      this.this$0.startSycn(2, paramList);
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      MyWordBookActivity.access$6(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
    }

    public void onServiceDisconnected()
    {
      MyWordBookActivity.access$6(this.this$0, null);
    }
  }
}